﻿using System;
using Rainbow.Exceptions;
using Rainbow.ObjectFlow.Language;

namespace Rainbow.ObjectFlow.Framework
{
    public static class Extensions
    {
        private static ExceptionHandler _exceptionHandler;
        private static object _workflow;

        public static Workflow<T> When<T>(this IHandle handler) where T : class
        {
            var workflow = _workflow as Workflow<T>;
            if (null == workflow)
                throw new InvalidCastException(string.Format("Couldn't cast to workflow<{0}>",typeof(T).ToString()));

            workflow.ExceptionHandler = _exceptionHandler;
            return workflow;
        }

        public static IHandle Configure<T>(this IDefine<T> workflow) where T : class
        {
            _workflow = workflow;
            _exceptionHandler = new ExceptionHandler();
            return _exceptionHandler;
        }

        public static IHandle Configure<T>(this Workflow<T> workflow) where T : class
        {
            _workflow = workflow;
            _exceptionHandler = new ExceptionHandler();
            return _exceptionHandler;
        }
    }
}